import {action, computed, observable, toJS} from "mobx";
import {Bubble, Tools} from "weaver-mobile";
import {Browser, BrowserHrm, ListViewPage} from "weaver-mobile-page";
import {accessResultApi, cycleDataApi} from "../../api/performance/accessResultApi";

const getLabel = Tools.getLabel;

/**
 * 考核成绩
 * jiansong 2019/11/19
 */
export default class AccessResultStore {

    @observable list = new ListViewPage.Store({route: "/performance/accessResult"});
    @observable subBrowser = new Browser.Store({route: "/performance/accessResult/condition"});
    @observable deptBrowser = new Browser.Store({route: "/performance/accessResult/condition"});
    @observable hrmBrowser = new BrowserHrm.Store({route: "/performance/accessResult/condition"});
    @observable subBrowserValue = [];
    @observable deptBrowserValue = [];
    @observable hrmBrowserValue = [];
    @observable stabList = []; //考核周期列表
    @observable hasInit=false; //是否已经初始化

    dataKey = ""; //获取列表数据key

    //查询条件
    @observable queryParams = {
        year: "",//年份
        type1: "1", //类型
        type2: "",  //月份或季度
        status: "", //状态
        hrmids: "", //人员
        isreset: "",//是否有更新
        minresult: "", //成绩最低
        maxresult: "", //成绩最高
        includesub: "3", //是否包含分部，默认含子分部
        subcompanyids: "", //分部
        includedept: "3", //是否包含部门，默认含子部门
        departmentids: "", //部门
    };

    //获取选中类型
    @computed get type() {
        let value = "";
        this.stabList.map(item => {
            if (item.key == this.queryParams.type1) {
                value = item.title;
            }
        })
        return value;
    }

    @action("初始化操作")
    doInit = () => {
        this.queryParams = {
            year: "",
            type1: "1",
            type2: "",
            status: "",
            hrmids: "",
            isreset: "",
            minresult: "",
            maxresult: "",
            includesub: "3",
            subcompanyids: "",
            includedept: "3",
            departmentids: "",
        }
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
        this.initData();
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("重置条件")
    resetCondition = () => {
        this.queryParams = { //查询条件
            ...this.queryParams,
            status: "", //状态
            hrmids: "", //人员
            isreset: "",//是否有更新
            minresult: "", //成绩最低
            maxresult: "", //成绩最高
            includesub: "3", //是否包含分部，默认含子分部
            subcompanyids: "", //分部
            includedept: "3", //是否包含部门，默认含子部门
            departmentids: "", //部门
        };
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
    }

    @action("更改数据")
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    @action("获取考核周期数据")
    initData = () => {
        cycleDataApi().then(action(res => {
            if (res.api_status) {
                this.stabList = res.stabList;
                if(this.stabList.length>0){
                    this.queryParams.type1=this.stabList[0].key
                }
                this.hasInit=true
                this.getTableDatas();
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }))
    };

    @action("渲染table数据")
    getTableDatas = () => {
        accessResultApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.list.clearData();
                this.queryParams.type1 = res.type1;
                this.queryParams.year = res.year;
                this.queryParams.type2 = Number(res.type2);
                const {datas: dataKey} = res;
                this.dataKey = dataKey
                this.list.getData({dataKey});
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }));
    };

    @action('更新列表数据数据')
    updateListData = (params) => {
        const {data, updateDataSource, setState} = this.list;
        let index = data.findIndex(item => item.scoreid == params.scoreid);
        if (index!=undefined) {
            Tools.callApi({
                url: '/api/ec/dev/table/datas',
                method: 'POST',
                params: {dataKey: this.dataKey, current: index + 1, pageSize: 1},
            }).then(res => {
                if (res.datas && res.datas[0]) {
                    data[index] = res.datas[0];
                    setState({data});
                    updateDataSource();
                }
            })
        }
    }

}